//
//  BTreeNode.m
//  SimulacraDB
//
//  Created by Andre Cohen on 8/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BTreeNode.h"
#import "IndexRecord.h"

@implementation BTreeNode

@synthesize keys;
@synthesize edges;

+ (BTreeNode *)createBTreeNode {
	return [[[BTreeNode alloc] init] autorelease];
}

- (id)init {
	[super init];
	edges = [[[NSMutableArray alloc] init] autorelease];
	keys = [[[NSMutableArray alloc] init] autorelease];
	return self;
}

- (id)initWithCoder:(NSCoder *)coder {
	[super init];
	if([coder containsValueForKey:@"keys"])
		keys = [[coder decodeObjectForKey:@"keys"] retain];
	if([coder containsValueForKey:@"edges"])
		edges = [[coder decodeObjectForKey:@"edges"] retain];
	else
		NSLog(@"Edges for BTree node not found");
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[coder encodeObject:keys forKey:@"keys"];
	[coder encodeObject:edges forKey:@"edges"];
}

- (BOOL)isLeaf {
	return [edges count]<2;
}

- (void)setSibling:(BTreeNode *)node {
	if(![self isLeaf]){
		NSLog(@"Error - cannot set sibling to non-leaf node");
		return;
	}
	[edges removeAllObjects];
	[edges addObject:node];
}

- (NSString *)description {
	NSMutableString *desc = [[[NSMutableString alloc] initWithString:@""] autorelease];
	int i;
	for(i=0;i<[keys count];i++)
		[desc appendFormat:@"%d ",[(IndexRecord *)[keys objectAtIndex:i] rid]];
	return desc;
}




@end
